package simtools.wizard;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;


public class NetworkTopology extends JInternalFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JPanel jPanel2 = null;

	private JPanel jPanel3 = null;

	private JPanel jPanel4 = null;

	private JPanel jPanel5 = null;

	private JTextField routerTextField = null;

	private JButton jButton = null;

	private JTextField jTextField1 = null;

	private JLabel jLabel = null;

	private JLabel jLabel2 = null;

	private JSlider jSlider = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JLabel jLabel5 = null;

	private JTextField jTextField3 = null;

	private JTextField jTextField4 = null;

	private JLabel jLabel6 = null;

	private JTextField jTextField5 = null;

	private JLabel jLabel7 = null;

	private JSlider jSlider1 = null;

	private JLabel jLabel8 = null;

	private JSlider jSlider2 = null;

	private JLabel jLabel9 = null;

	private JLabel jLabel10 = null;

	private JRadioButton jRadioButton = null;

	private JRadioButton jRadioButton1 = null;

	private JRadioButton jRadioButton2 = null;

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	private JLabel jLabel11 = null;

	private JTextField jTextField6 = null;

	private JLabel jLabel12 = null;

	private JLabel jLabel13 = null;

	private JSlider jSlider3 = null;

	private JCheckBox jCheckBox = null;

	private JRadioButton jRadioButton4 = null;

	private JRadioButton jRadioButton5 = null;
	
	public static NetworkTopology getInstance() {
		return NetworkTopologyHolder.instance;
	}
	
	private static class NetworkTopologyHolder {
        private static NetworkTopology instance = new NetworkTopology();
    }
	
	/**
	 * This is the default constructor
	 */
	public NetworkTopology() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(642, 460);
		this.setClosable(true);
		this.setContentPane(getJContentPane());
		this.setTitle("Network Topology");
		this.setResizable(false);
		Wizard.centralize(this);
		
		jTextField1.setEnabled(false);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJPanel1(), null);
			jContentPane.add(getJPanel2(), null);
			jContentPane.add(getJPanel3(), null);
			jContentPane.add(getJPanel4(), null);
			jContentPane.add(getJPanel5(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(15, 61, 109, 20));
			jLabel.setText("Link speed in Mbits/sec");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(15, 15, 346, 90));
			jPanel.setBorder(BorderFactory.createTitledBorder(null, "Routers", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), new Color(0, 70, 213)));
			jPanel.add(getRouterTextField(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJTextField1(), null);
			jPanel.add(jLabel, null);
			jPanel.add(getJCheckBox(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(13, 161, 132, 20));
			jLabel5.setText("Mean waiting time per retry");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(13, 118, 84, 20));
			jLabel4.setText("Number of retries");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(14, 54, 121, 20));
			jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel3.setText("500");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(14, 29, 120, 20));
			jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel2.setText("Outgoing Connections");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBounds(new Rectangle(374, 15, 241, 193));
			jPanel1.setBorder(BorderFactory.createTitledBorder(null, "Clients", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), new Color(0, 70, 213)));
			jPanel1.add(jLabel2, null);
			jPanel1.add(getJSlider(), null);
			jPanel1.add(jLabel3, null);
			jPanel1.add(jLabel4, null);
			jPanel1.add(jLabel5, null);
			jPanel1.add(getJTextField3(), null);
			jPanel1.add(getJTextField4(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jLabel10 = new JLabel();
			jLabel10.setBounds(new Rectangle(210, 84, 120, 20));
			jLabel10.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel10.setText("500");
			jLabel9 = new JLabel();
			jLabel9.setBounds(new Rectangle(15, 84, 120, 20));
			jLabel9.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel9.setText("500");
			jLabel8 = new JLabel();
			jLabel8.setBounds(new Rectangle(210, 59, 120, 20));
			jLabel8.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel8.setText("Incoming Connections");
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(15, 59, 120, 20));
			jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel7.setText("Outgoing Connections");
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(15, 24, 106, 20));
			jLabel6.setText("Number of Surrogates");
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			jPanel2.setBounds(new Rectangle(15, 112, 346, 143));
			jPanel2.setBorder(BorderFactory.createTitledBorder(null, "Surrogate Servers", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), new Color(0, 70, 213)));
			jPanel2.add(jLabel6, null);
			jPanel2.add(getJTextField5(), null);
			jPanel2.add(jLabel7, null);
			jPanel2.add(getJSlider1(), null);
			jPanel2.add(jLabel8, null);
			jPanel2.add(getJSlider2(), null);
			jPanel2.add(jLabel9, null);
			jPanel2.add(jLabel10, null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jLabel13 = new JLabel();
			jLabel13.setBounds(new Rectangle(15, 96, 120, 20));
			jLabel13.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel13.setText("500");
			jLabel12 = new JLabel();
			jLabel12.setBounds(new Rectangle(15, 71, 120, 20));
			jLabel12.setText("Incoming Connections");
			jLabel11 = new JLabel();
			jLabel11.setBounds(new Rectangle(15, 27, 90, 20));
			jLabel11.setText("Number of Servers");
			jPanel3 = new JPanel();
			jPanel3.setLayout(null);
			jPanel3.setBounds(new Rectangle(374, 217, 241, 154));
			jPanel3.setBorder(BorderFactory.createTitledBorder(null, "Origin Servers", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), new Color(0, 70, 213)));
			jPanel3.add(jLabel11, null);
			jPanel3.add(getJTextField6(), null);
			jPanel3.add(jLabel12, null);
			jPanel3.add(jLabel13, null);
			jPanel3.add(getJSlider3(), null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(1);
			gridLayout1.setHgap(10);
			jPanel4 = new JPanel();
			jPanel4.setLayout(gridLayout1);
			jPanel4.setBounds(new Rectangle(238, 389, 156, 26));
			jPanel4.add(getJButton1(), null);
			jPanel4.add(getJButton2(), null);
		}
		return jPanel4;
	}

	/**
	 * This method initializes jPanel5	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel5() {
		ButtonGroup placementGroup = new ButtonGroup();
		if (jPanel5 == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(3);
			gridLayout.setColumns(0);
			gridLayout.setHgap(0);
			jPanel5 = new JPanel();
			jPanel5.setLayout(gridLayout);
			jPanel5.setBounds(new Rectangle(15, 258, 346, 113));
			jPanel5.setBorder(BorderFactory.createTitledBorder(null, "Placement", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), new Color(0, 70, 213)));
			jPanel5.add(getJRadioButton4(), null);
			jPanel5.add(getJRadioButton(), null);
			jPanel5.add(getJRadioButton1(), null);
			jPanel5.add(getJRadioButton5(), null);
			jPanel5.add(getJRadioButton2(), null);
			
			placementGroup.add(getJRadioButton());
			placementGroup.add(getJRadioButton1());
			placementGroup.add(getJRadioButton2());
			placementGroup.add(getJRadioButton4());
			placementGroup.add(getJRadioButton5());
		}
		return jPanel5;
	}
	
	public String getPlacement() {
		if (jRadioButton4.isSelected())
			return "Random";
		else if (jRadioButton1.isSelected())
			return "Tree-based";
		else if (jRadioButton2.isSelected())
			return "Hotspot";
		else if (jRadioButton.isSelected())
			return "Greedy";
		else 
			return "Topology-informed";
	}

	/**
	 * This method initializes routerTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getRouterTextField() {
		if (routerTextField == null) {
			routerTextField = new JTextField();
			routerTextField.setBounds(new Rectangle(15, 30, 252, 20));
		}
		return routerTextField;
	}
	
	public String getRouterPath() {
		return routerTextField.getText();
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(271, 28, 63, 24));
			jButton.setText("Open");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser jFileChooser = new JFileChooser();  
					int status = jFileChooser.showOpenDialog(null);  
					if (status == JFileChooser.APPROVE_OPTION) { 
					  String selectedPath = jFileChooser.getSelectedFile().getAbsolutePath();
					  routerTextField.setText(selectedPath);
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(129, 61, 41, 20));
			jTextField1.setText("1000");
		}
		return jTextField1;
	}
	
	public int getDefaultLinkSpeed() {
		if (jCheckBox.isSelected())
			return Integer.parseInt(jTextField1.getText());
		else
			return 0;
	}

	/**
	 * This method initializes jSlider	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSlider() {
		if (jSlider == null) {
			jSlider = new JSlider();
			jSlider.setBounds(new Rectangle(14, 79, 120, 20));
			jSlider.setPaintLabels(true);
			jSlider.setPaintTicks(true);
			jSlider.setMaximum(1000);
			jSlider.setValue(500);
			
			jSlider.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					jLabel3.setText("" + jSlider.getValue());
				}
			});
		}
		return jSlider;
	}
	
	public int getClientOutgoingConnections() {
		return jSlider.getValue();
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setBounds(new Rectangle(102, 118, 42, 20));
			jTextField3.setText("10");
		}
		return jTextField3;
	}
	
	public int getNumberOfRetries() {
		return Integer.parseInt(jTextField3.getText());
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setBounds(new Rectangle(150, 161, 35, 20));
			jTextField4.setText("5");
		}
		return jTextField4;
	}
	
	public int getMeanWaitingTimePerRetry() {
		return Integer.parseInt(jTextField4.getText());
	}

	/**
	 * This method initializes jTextField5	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField5() {
		if (jTextField5 == null) {
			jTextField5 = new JTextField();
			jTextField5.setBounds(new Rectangle(126, 24, 40, 20));
			jTextField5.setText("100");
		}
		return jTextField5;
	}
	
	public int getNumberOfSurrogates() {
		return Integer.parseInt(jTextField5.getText());
	}

	/**
	 * This method initializes jSlider1	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSlider1() {
		if (jSlider1 == null) {
			jSlider1 = new JSlider();
			jSlider1.setBounds(new Rectangle(15, 109, 120, 20));
			jSlider1.setPaintLabels(true);
			jSlider1.setPaintTicks(true);
			jSlider1.setMaximum(1000);
			jSlider1.setValue(500);
			jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					jLabel9.setText("" + jSlider1.getValue());
				}
			});
		}
		return jSlider1;
	}
	
	public int getSurrogateOutgoingConnection() {
		return jSlider1.getValue();
	}

	/**
	 * This method initializes jSlider2	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSlider2() {
		if (jSlider2 == null) {
			jSlider2 = new JSlider();
			jSlider2.setBounds(new Rectangle(210, 109, 120, 20));
			jSlider2.setPaintLabels(true);
			jSlider2.setPaintTicks(true);
			jSlider2.setMaximum(1000);
			jSlider2.setValue(500);
			jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					jLabel10.setText("" + jSlider2.getValue());
				}
			});
		}
		return jSlider2;
	}
	
	public int getSurrogateIncomingConnections() {
		return jSlider2.getValue();
	}

	/**
	 * This method initializes jRadioButton	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setText("Greedy");
		}
		return jRadioButton;
	}

	/**
	 * This method initializes jRadioButton1	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setText("Tree-based");
		}
		return jRadioButton1;
	}

	/**
	 * This method initializes jRadioButton2	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton2() {
		if (jRadioButton2 == null) {
			jRadioButton2 = new JRadioButton();
			jRadioButton2.setText("Hotspot");
		}
		return jRadioButton2;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("< Back");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Wizard.jDesktopPane.removeAll();
					Wizard.jDesktopPane.repaint();
					
					Wizard.internal = CdnPolicy.getInstance();
					Wizard.jDesktopPane.add(Wizard.internal);
					Wizard.internal.setVisible(true);
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Next >");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Wizard.jDesktopPane.removeAll();
					Wizard.jDesktopPane.repaint();
					
					Wizard.internal = Dataset.getInstance();
					Wizard.jDesktopPane.add(Wizard.internal);
					Wizard.internal.setVisible(true);
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jTextField6	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField6() {
		if (jTextField6 == null) {
			jTextField6 = new JTextField();
			jTextField6.setBounds(new Rectangle(110, 27, 41, 20));
			jTextField6.setText("1");
		}
		return jTextField6;
	}
	
	public int getNumberOfOriginServers() {
		return Integer.parseInt(jTextField6.getText());
	}

	/**
	 * This method initializes jSlider3	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSlider3() {
		if (jSlider3 == null) {
			jSlider3 = new JSlider();
			jSlider3.setBounds(new Rectangle(15, 121, 120, 18));
			jSlider3.setPaintTicks(true);
			jSlider3.setPaintLabels(true);
			jSlider3.setMaximum(1000);
			jSlider3.setValue(500);
			jSlider3.addChangeListener(new javax.swing.event.ChangeListener() {
				public void stateChanged(javax.swing.event.ChangeEvent e) {
					jLabel13.setText("" + jSlider3.getValue());
				}
			});
			
		}
		return jSlider3;
	}
	
	public int getOriginServerIncomingConnections() {
		return jSlider3.getValue();
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setBounds(new Rectangle(176, 61, 138, 20));
			jCheckBox.setText("Default");
			jCheckBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (jCheckBox.isSelected()) {
						jTextField1.setEnabled(true);
					} else {
						jTextField1.setEnabled(false);
					}
				}
			});
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jRadioButton4	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton4() {
		if (jRadioButton4 == null) {
			jRadioButton4 = new JRadioButton();
			jRadioButton4.setText("Random");
			jRadioButton4.setSelected(true);
		}
		return jRadioButton4;
	}

	/**
	 * This method initializes jRadioButton5	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton5() {
		if (jRadioButton5 == null) {
			jRadioButton5 = new JRadioButton();
			jRadioButton5.setText("Topology-informed");
		}
		return jRadioButton5;
	}

}  //  @jve:decl-index=0:visual-constraint="19,6"
